package cn.xxc.basic.service.impl;

import cn.xxc.basic.exception.BusinessException;
import cn.xxc.basic.service.IVerifyService;
import cn.xxc.basic.util.AjaxResult;
import cn.xxc.basic.util.StrUtils;
import cn.xxc.basic.util.VerifyCodeUtils;
import cn.xxc.user.domain.User;
import cn.xxc.user.mapper.UserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 发送验证码
 *      1.图形验证码
 *      2.手机注册短信验证码
 *      3.微信绑定短信验证码
 */
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class IVerifyServiceImpl implements IVerifyService {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserMapper userMapper;

    /**
     * 图像验证码
     * @param key
     * @return
     */
    @Override
    public String imageVerifyCode(String key) {
        String s = StrUtils.getComplexRandomString(4);
        redisTemplate.opsForValue().set(key, s,3, TimeUnit.MINUTES);
        return VerifyCodeUtils.verifyCode(115, 40, s);
    }

    /**
     * 手机注册 发送手机验证码
     * @param map
     */
    @Override
    public void phoneVerifyCode(Map<String, String> map) {
        String phone = map.get("phone");
        String imageCode = map.get("imageCode");
        String key = map.get("key");
        //一：校验：空值校验 - 前端和后端都可以校验手机格式问题
        if (StringUtils.isEmpty(phone)|| StringUtils.isEmpty(imageCode)){
            throw new BusinessException("信息不能为空");
        }

        //二：校验：图形验证码 - 是否过期，是否正确
        Object o = redisTemplate.opsForValue().get(key);
        if(o==null){
            throw new BusinessException("图形验证码过期");
        }
        if (!imageCode.equalsIgnoreCase(o.toString())) {
            throw new BusinessException("图形验证码错误");
        }
        //三：校验：手机号码是否注册过
        User user=userMapper.findByPhone(phone);
        if (user != null) {
            throw new BusinessException("手机号码已注册,请直接登录！");
        }
        //四：直接从redis获取手机验证码：key【业务:手机号】
        Object o1 = redisTemplate.opsForValue().get("register:" + phone);
        String code = null;//用于保存手机验证码
        if (o1==null){
            code=StrUtils.getRandomString(4);
        }else {
            long newTime = System.currentTimeMillis();
            long oldTime= Long.parseLong(o1.toString().split(":")[1]);
            if ((newTime-oldTime)<1*60*1000){
                throw new BusinessException("操作频繁，稍后重试!");
            }else {
                code=o1.toString().split(":")[0];
            }
        }
        redisTemplate.opsForValue().set("register:" + phone, code+":"+System.currentTimeMillis(),5,TimeUnit.MINUTES);
        //六：调用短信的工具类发送短信
        //SmsUtils.sendSms(phone,"你的手机验证码是：" + code +",请在3分钟内使用!!!");
        System.out.println("你的手机验证码是：" + code +",请在3分钟内使用!!!");
    }

    /**
     * 微信手机号绑定发送手机验证码
     * @param phone
     */
    @Override
    public void binderSmsCode(String phone) {
        //1.空值校验
        if (StringUtils.isEmpty(phone)){
            throw new BusinessException("信息不能为空");
        }
        //2.手机号码校验
        User user=userMapper.findByPhone(phone);
        if (user != null) {
            throw new BusinessException("手机号码已注册,请直接登录！");
        }
        //3.手机验证码
        Object o1 = redisTemplate.opsForValue().get("binder:" + phone);
        String code = null;//用于保存手机验证码
        if (o1==null){
            code=StrUtils.getRandomString(4);
        }else {
            long newTime = System.currentTimeMillis();
            long oldTime= Long.parseLong(o1.toString().split(":")[1]);
            if ((newTime-oldTime)<1*60*1000){
                throw new BusinessException("操作频繁，稍后重试!");
            }else {
                code=o1.toString().split(":")[0];
            }
        }
        redisTemplate.opsForValue().set("binder:" + phone, code+":"+System.currentTimeMillis(),5,TimeUnit.MINUTES);
        //六：调用短信的工具类发送短信
        //SmsUtils.sendSms(phone,"你的手机验证码是：" + code +",请在3分钟内使用!!!");
        System.out.println("你的手机验证码是：" + code +",请在3分钟内使用!!!");
    }

}
